package com.stone.boot.pipeline.domain.entity;

import com.stone.boot.pipeline.domain.repository.PipelineNodeRepository;
import io.choerodon.mybatis.annotation.ModifyAudit;
import io.choerodon.mybatis.annotation.VersionAudit;
import io.choerodon.mybatis.domain.AuditDomain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.util.Assert;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

/**
 * 流程器节点
 *
 * @author Mr_wenpan@163.com 2021/8/5 9:54 下午
 */
@Data
@ApiModel("流程器节点")
@VersionAudit
@ModifyAudit
@Table(name = "o2pl_pipeline_node")
@EqualsAndHashCode(callSuper = true)
public class PipelineNode extends AuditDomain {
    public static final String FIELD_ID = "id";
    public static final String FIELD_PIPELINE_ID = "pipelineId";
    public static final String FIELD_CUR_ACTION = "curAction";
    public static final String FIELD_STRATEGY_TYPE = "strategyType";
    public static final String FIELD_NEXT_ACTION = "nextAction";
    public static final String FIELD_CUR_DESCRIPTION = "curDescription";

    @ApiModelProperty("表ID，主键，供其他表做外键")
    @Id
    @GeneratedValue
    private Long id;
    @ApiModelProperty(value = "流水线ID")
    private Long pipelineId;
    @ApiModelProperty(value = "当前行为")
    @NotNull
    private String curAction;
    @ApiModelProperty(value = "流水线节点描述")
    private String curDescription;
    @ApiModelProperty(value = "下个节点行为")
    @NotNull
    private String nextAction;
    @ApiModelProperty(value = "决策类型，值集 OBSM.PIPELINE_STRATEGY")
    private String strategyType;
    @Transient
    private String strategyTypeMeaning;

    //
    // 业务方法(按public protected private顺序排列)
    // ------------------------------------------------------------------------------

    /**
     * todo 这里需要批量校验优化，pipelineId + strategyType + curAction 为数据库唯一索引
     */
    public boolean exist(PipelineNodeRepository pipelineNodeRepository) {
        Sqls sql = Sqls.custom();
        if (id != null) {
            sql.andNotEqualTo(PipelineNode.FIELD_ID, id);
        }
        if (pipelineId != null) {
            sql.andEqualTo(PipelineNode.FIELD_PIPELINE_ID, pipelineId);
        }
        if (StringUtils.isNotBlank(curAction)) {
            sql.andEqualTo(PipelineNode.FIELD_CUR_ACTION, curAction);
        }
        if (StringUtils.isNotBlank(strategyType)) {
            sql.andEqualTo(PipelineNode.FIELD_STRATEGY_TYPE, strategyType);
        }
        return pipelineNodeRepository.selectCountByCondition(Condition.builder(PipelineNode.class).andWhere(sql).build()) > 0;
    }

    /**
     * 字段空校验
     *
     * @author Mr_wenpan@163.com 2021/8/5 10:24 下午
     */
    public void validate() {
        Assert.notNull(curAction, "当前行为不能为空");
        Assert.notNull(strategyType, "决策类型不能为空");
        Assert.notNull(pipelineId, "关联流水线ID不能为空");
    }

}